package com.bci.pwtz.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;

import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.ProductionMapper;
import com.bci.pwtz.mysql.model.Production;
import com.bci.pwtz.service.ProductionService;

public class ProductionServiceImpl implements ProductionService{

	private ProductionMapper objMapper;
	 private static final Logger _logger=Logger.getLogger(ProductionServiceImpl.class);
	public ProductionMapper getObjMapper() {
		return objMapper;
	}

	public void setObjMapper(ProductionMapper objMapper) {
		this.objMapper = objMapper;
	}

	@Override
	public int insert(Production pro) throws PwtzDatabaseException{
		 if (_logger.isDebugEnabled())
	        {
	            _logger.debug("add: production=" + pro.getName());
	        }
		int result =0 ;
		try {
			objMapper.insert(pro);
			result=1;
		} catch (DataAccessException e) {
			 _logger.error(ExceptionUtils.getStackTrace(e));
	            throw new PwtzDatabaseException();
		}
		return result;
	}

	@Override
	public int deleleById(int id) throws PwtzDatabaseException{
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int update(Production pro) throws PwtzDatabaseException{
		if (_logger.isDebugEnabled())
        {
            _logger.debug("update : production=" + pro.getName());
        }
		int result =0 ;
		try {
			objMapper.update(pro);
			result = 1;
		} catch (DataAccessException e) {
			 _logger.error(ExceptionUtils.getStackTrace(e));
	            throw new PwtzDatabaseException();
		}
		return result;
	}

	@Override
	public Production load(int id) throws PwtzDatabaseException {
		Production pro = null;
		try {
			pro = objMapper.load(id);
		} catch (DataAccessException e) {
			 _logger.error(ExceptionUtils.getStackTrace(e));
	            throw new PwtzDatabaseException();
		}
		return pro;
	}

	@Override
	public List<Production> getProList() throws  PwtzDatabaseException{
		List<Production> list= new ArrayList<Production>();
		try {
			list = objMapper.getProList();
		} catch (DataAccessException e) {
			 _logger.error(ExceptionUtils.getStackTrace(e));
	            throw new PwtzDatabaseException();
		}
		return list;
	}

}
